package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.BrewLabDetails;

public class BrewLabDetailsPresenter extends AbstractPresenter<BrewLabDetails> {
	final String SINGLEITEM = "brewLabDetails";
	final String MULTIITEM = "brewLabDetailss";
	
	public ArrayList<BrewLabDetails> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public BrewLabDetails tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public BrewLabDetails createItemFromJSONObject(JSONObject jsonObject){
		BrewLabDetails item = new BrewLabDetails();
		try{
			item.setId(jsonObject.getInt("bldid"));
			item.setNotes(jsonObject.getString("bldNotes"));
			item.setDate(getDateFromString(jsonObject.getString("date")));
			item.setDescription(jsonObject.getString("blddesc"));
			item.setResult(jsonObject.getInt("bldResult"));
			
			LabTypePresenter ltPresenter = new LabTypePresenter();
			JSONObject jsonFGI = jsonObject.getJSONObject("labType");
			item.setLabType(ltPresenter.createItemFromJSONObject(jsonFGI));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(BrewLabDetails item){
		try{
			JSONObject json = new JSONObject();
			json.put("bldid", item.getId());
			json.put("bldNotes", item.getNotes());
			json.put("date", getDate(item.getDate()));
			json.put("blddesc", item.getDescription());
			json.put("bldResult", item.getResult());
			
			LabTypePresenter ltPresenter = new LabTypePresenter();
			json.put("labType", ltPresenter.createJSON(item.getLabType()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public BrewLabDetails getEmptyEntity(){
		BrewLabDetails entity = new BrewLabDetails();
		return entity;
	}
}
